package com.dcjt.firehotcarpad.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;

/**
 * Created by cj on 2021/2/28.
 * Email:codesexy@163.com
 * Function:
 * desc:title上使用的Span
 */
public class BackgroundColorSpan extends ReplacementSpan {
    private int bgColor;
    private int textColor;

    public BackgroundColorSpan(int bgColor, int textColor) {
        super();
        this.bgColor = bgColor;
        this.textColor = textColor;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return ((int) paint.measureText(text, start, end) + 40);
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        int color1 = paint.getColor();
        paint.setColor(this.bgColor);
        canvas.drawRoundRect(new RectF(x, top + 1, x + ((int) paint.measureText(text, start, end) + 30), bottom - 1), 10, 10, paint);
        paint.setColor(this.textColor);
        canvas.drawText(text, start, end, x + 15, y, paint);
        paint.setColor(color1);
    }
}